package com.danger.Model.action;

import android.content.Context;

import com.danger.Model.bean.ObjectInfo;
import com.danger.Model.bean.UserInfo;
import com.danger.Model.listener.OnGetListener;
import com.danger.Util.CheckCode;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by danger on 2016/3/30.
 */
public class ObjectInfoModelImpl implements ObjectInfoModel {


    private Context context;

    public ObjectInfoModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void getObjectInfo(final OnGetListener listener) {
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(CheckCode.checkByCode(i));
            }
        });
    }


    @Override
    public void getMyFoundObjectInfo(final OnGetListener listener) {
        UserInfo userInfo = BmobUser.getCurrentUser(context, UserInfo.class);
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.addWhereEqualTo("name", userInfo.getUsername());
        query.addWhereEqualTo("lost", false);
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(CheckCode.checkByCode(i));
            }
        });
    }

    @Override
    public void getMyLostObjectInfo(final OnGetListener listener) {
        UserInfo userInfo = BmobUser.getCurrentUser(context, UserInfo.class);
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.addWhereEqualTo("name", userInfo.getUsername());
        query.addWhereEqualTo("lost", true);
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(CheckCode.checkByCode(i));
            }
        });
    }

    @Override
    public void getFoundObjectInfo(final OnGetListener listener) {
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.addWhereEqualTo("lost", false);
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(CheckCode.checkByCode(i));
            }
        });
    }

    @Override
    public void getLostObjectInfo(final OnGetListener listener) {
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.addWhereEqualTo("lost", true);
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(CheckCode.checkByCode(i));
            }
        });
    }
}
